package com.weharvest.edu.controller;

import com.weharvest.edu.annotation.MenuAnnotion;
import com.weharvest.edu.annotation.PermissionAnnotion;
import com.weharvest.edu.annotation.SignSecurity;
import com.weharvest.edu.constant.MenuConstant;
import com.weharvest.edu.constant.PermissionConstants;
import com.weharvest.edu.dto.BackstageLoginDTO;
import com.weharvest.edu.enums.ResultEnum;
import com.weharvest.edu.service.EduService;
import com.weharvest.edu.utlis.ResultUtil;
import com.weharvest.edu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/edu")
@Slf4j
public class EduController {

    @Resource
    private EduService eduService;

    /**
     * 主页
     *
     * @return
     */
//    @Permission
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(
            value = "openid", defaultValue = "oLM2J1aJTxVb85KXM39a66ftOGmk") String openid) {
        // 获取所有菜单
        return new ModelAndView("index");
    }

    //后台用户登录
    @ResponseBody
    @PostMapping("/login")
    public ResultVo Login(@RequestBody BackstageLoginDTO backstageLoginDTO, HttpServletRequest request) {
        String token = eduService.login(backstageLoginDTO, request);
        if (token == null || token.isEmpty()) {
            return ResultUtil.success(ResultEnum.LOGIN_ERROR);
        }
        return ResultUtil.success(token);
    }

    //兑换用户信息
    @ResponseBody
    @PostMapping("/detail")
    @SignSecurity
    @MenuAnnotion(MenuConstant.BACKSTAGE_USER)
    @PermissionAnnotion(PermissionConstants.DATA_FIND)
    public ResultVo detail(HttpServletRequest request, HttpServletResponse response) {
        return ResultUtil.success(eduService.detail(request, response));
    }

}
